package com.cloudcc.mobile.view.customer;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.mobile.AppManager;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.FragmentViewPagerAdapter;
import com.cloudcc.mobile.entity.map.MapLocation;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.activity.EditorActivity;

/**
 * 客户列表类
 * 
 * @author Administrator
 * @deprecated
 * 
 */
public class CustomerActivity extends FragmentActivity implements
		OnClickListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private ImageView ivBottomLine;
	private LinearLayout.LayoutParams imagebtn_params;
	private int position_one;
	private ViewPager viewpager;
	private TextView kehu;
	private TextView qzkehu;
	private TextView lianxiren;
	private ArrayList<BaseCustomerListFragment> list;
	private int currIndex = 0;
	private Context context;
	private ImageView send;
	private MapLocation location;
	private ImageView fanhuikehu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.customers);
			AppManager.getInstance().push(this);
			context = this;
			InitWidth();
			setupView();// 获取控件
			setData();// 初始化界面
			addListener();
		} catch (Exception e) {
			Tools.handle(e);
		}
	}

	private void addListener() {
		fanhuikehu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CustomerActivity.this.finish();
			}
		});
		send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CustomerActivity.this,
						EditorActivity.class);
				if ("en".equals(mEns)) {
					intent.putExtra("CustomerActivity",
							"Sign in: I am here【" + location.getAddress() + "】");
					Log.d("shifoucongzheli", "Sign in: I am here【" + location.getAddress()
							+ "】" + 0);
					}else{
						intent.putExtra("CustomerActivity",
								"签到:我在这里【" + location.getAddress() + "】");
						Log.d("shifoucongzheli", "签到:我在这里【" + location.getAddress()
								+ "】" + 0);
					}
				
				intent.putExtra("urlId", "null");
				intent.putExtra("MapsActivity", location);
				intent.putExtra("key", "-1");
				startActivity(intent);
			}
		});

	}

	private void setData() {
		list = new ArrayList<BaseCustomerListFragment>();
		BaseCustomerListFragment fragment1 = new LeadListFragment();
		BaseCustomerListFragment fragment2 = new CustomerListFragment();
		BaseCustomerListFragment fragment3 = new ContactListFragment();
		list.add(fragment1);
		list.add(fragment2);
		list.add(fragment3);
		FragmentViewPagerAdapter adapter = new FragmentViewPagerAdapter(
				getSupportFragmentManager(), viewpager, list);
		adapter.setOnExtraPageChangeListener(new FragmentViewPagerAdapter.OnExtraPageChangeListener() {

			@Override
			public void onExtraPageScrolled(int i, float v, int i2) {
				imagebtn_params.leftMargin = (int) ((i + v) * position_one);
				ivBottomLine.requestLayout();

				BaseCustomerListFragment fragment = list.get(i);
				fragment.onNowResume();

			}

			@Override
			public void onExtraPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					if (currIndex == 1) {
						kehu.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					} else if (currIndex == 2) {
						lianxiren.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					}
					qzkehu.setTextColor(context.getResources().getColor(
							R.color.lvse));
					break;
				case 1:
					if (currIndex == 0) {
						qzkehu.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					} else if (currIndex == 2) {
						lianxiren.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					}
					kehu.setTextColor(context.getResources().getColor(
							R.color.lvse));
					break;
				case 2:
					if (currIndex == 0) {
						qzkehu.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					} else if (currIndex == 1) {
						kehu.setTextColor(context.getResources().getColor(
								R.color.shenhui));
					}
					lianxiren.setTextColor(context.getResources().getColor(
							R.color.lvse));
					break;
				}
				currIndex = arg0;

				super.onExtraPageSelected(arg0);
			}
		});
		viewpager.setAdapter(adapter);
		viewpager.setCurrentItem(0);
		viewpager.setOffscreenPageLimit(1);
	}

	private void setupView() {
		location = (MapLocation) getIntent().getSerializableExtra(
				"MapsActivity");
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		qzkehu = (TextView) findViewById(R.id.qzkehu);
		kehu = (TextView) findViewById(R.id.kehu);
		lianxiren = (TextView) findViewById(R.id.lianxiren);
		send = (ImageView) findViewById(R.id.customers_send);
		fanhuikehu = (ImageView) findViewById(R.id.fanhuikehu);
		qzkehu.setOnClickListener(this);
		kehu.setOnClickListener(this);
		lianxiren.setOnClickListener(this);
		viewpager.setOffscreenPageLimit(0);
	}

	// 设置滑动图片的宽度
	private void InitWidth() {
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		// 获取屏幕的尺寸
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		// 设置滑动图片的宽与高
		imagebtn_params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		imagebtn_params.height = 7;
		imagebtn_params.width = dm.widthPixels / 3;
		ivBottomLine.setLayoutParams(imagebtn_params);
		position_one = (int) (screenW / 3.0);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppManager.getInstance().pop(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.qzkehu:
			viewpager.setCurrentItem(0);
			break;
		case R.id.kehu:
			viewpager.setCurrentItem(1);
			break;
		case R.id.lianxiren:
			viewpager.setCurrentItem(2);
			break;

		default:
			break;
		}
	}
}
